/**
 * 在vite.config.ts中添加
 *   resolve: {
 *       alias: {
 *         '@': resolve(__dirname, './'),
 *         '~@': resolve(__dirname, './'),
 *       }
 *     }
 * @type {string[][]}
 */
// 配置别名，当前文件向上两级作为跟路径使用 ../../这个路径为跟路径
const BrowseVueLoaderAliasPathConfig = [
  ['@/',''], // js就可以用了
  ['~@/',''], // 有这个是因为css里面用，
  // ['@bigdata/','bigdata'],
]


let BrowseVueLoaderPathAliasJsPath = document.currentScript.src

function pathJoin(jsFilePath, relativePath) {
  let origin = location.origin
  let strings = jsFilePath.replace(origin,'').split('/');
  if (relativePath.startsWith('./')) {
    relativePath = relativePath.substring(2)
  }
  strings[strings.length - 1] = relativePath
  relativePath = strings.join('/')
  strings = relativePath.split('/');
  for (let i = 0; i < strings.length; i++) {
    const item = strings[i]
    if (item === '..') { // 如果匹配到这个，把上一个也一起删除 上一个就是最近的没有删除的
      if (i > 0) {
        for (let j = i - 1; j > 0; j--) {
          if (strings[j] && strings[j] !== '.') {
            strings[i] = ''
            strings[j] = ''
            break
          }
        }
      }
    }
  }
  relativePath = strings.filter(i => i).join('/')
  return origin + '/' + relativePath
}
// @符号别名路径，也就是vueCommon的上一级
let basePath = pathJoin(BrowseVueLoaderPathAliasJsPath,"../../").replace(location.origin,"");

let newRewriteReplaceUrl=(newUrl,baseUrl,url)=>{
  return newUrl
}

BrowseVueLoader.rewriteReplaceUrl((newUrl,baseUrl,url)=>{
  for (let browseVueLoaderAliasPathConfigElement of BrowseVueLoaderAliasPathConfig) {
    if(url.startsWith(browseVueLoaderAliasPathConfigElement[0])){
      newUrl = basePath+browseVueLoaderAliasPathConfigElement[1]+url.substring(browseVueLoaderAliasPathConfigElement[0].length)
    }
  }
  return newRewriteReplaceUrl(newUrl,baseUrl,url)
})

BrowseVueLoader.rewriteReplaceUrl=(call)=>{
  newRewriteReplaceUrl=call
}
